<?php
declare (strict_types = 1);

namespace app\login\middleware;

use app\login\validate\User;
use think\facade\Cache;
use think\exception\ValidateException;

class Check
{
    /**
     * 处理请求
     *
     * @param \think\Request $request
     * @param \Closure       $next
     * @return Response
     */
    public function handle($request, \Closure $next)
    {
        //
        $data = input();
        $num = Cache::get('captcha');
//        dd($num);
        try {
            validate(User::class)->check($data);
        } catch (ValidateException $e) {
            // 验证失败 输出错误信息
            echo $e->getError();
            return redirect('form');
        }
        if ($data['captcha']!=$num) {
            return redirect('form');
        }

        return $next($request);
    }
}
